<template>
  <div class="addAreaContainer" v-if="isAddAreaShow" v-drag>
    <div class="addAreaTitle drag-handle">
      <div class="title-left">
        <div class="bar"></div>
        <div class="title-text">新增区域</div>
      </div>
      <div class="title-right">
        <!-- <div  tabindex="0" class="icon">-</div> -->
        <div tabindex="0" class="icon" @click="closeDialog">×</div>
      </div>
    </div>
    <div class="addAreaContent">
      <div class="dialog-content">
        <input v-model="newAreaName" type="text" placeholder="输入区域名称" />
        <input v-model="newContent" type="text" placeholder="输入区域备注" />
        <button @click="addArea">添加</button>
        <button @click="closeDialog">取消</button>
      </div>
    </div>
  </div>
</template>

<script>
import { insertQueryTemplate } from "@/api/EquipmentManagement.js";
export default {
  name: "AddArea",
  props: {
    dialogVisible: {
      type: Boolean,
      required: true,
    },
    areaData: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      newAreaName: "",
      newContent: "",
      map: null,
    };
  },
  computed: {
    isAddAreaShow() {
      // 返回 prop 的值
      return this.dialogVisible;
    },
  },
  mounted() {
    this.initMap();
  },

  methods: {
    initMap() {
      setTimeout(() => {
        this.map = this.$store.getters.Map;
        if (!this.map)  {
          console.error("地图实例未找到");
        }
      }, 1000); // 延迟 1000 毫秒（1秒）后执行
    },
    addArea() {
      insertQueryTemplate({
        areaName: this.newAreaName,
        areaRemark: this.newContent,
        areaValue: this.areaData,
      }).then((res) => {
        if (res.code == 200) {
          this.$message({
            showClose: true,
            message: res.message,
            type: "success",
          });
          this.$emit("updateTemplateData")
        }
      });
    
      this.clearAllLayers();
      this.closeDialog();
    },
    clearAllLayers() {
      this.map.interaction.clearPolygon();
    },
    closeDialog() {
      this.clearAllLayers();
      // 通过修改 prop 来关闭弹窗
      this.$emit("update:dialogVisible", false);
    },
  },
};
</script>

<style lang="scss" scoped>
.addAreaContainer {
  width: 25vw;
  color: white;
  position: absolute;
  top: 15vh;
  right: 45vw;
  height: 46vh;
  z-index: 2;
  .addAreaTitle {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    font-size: 1rem;
    background-color: #285383;
    .title-left,
    .title-right {
      display: flex;
      align-items: center;
    }
    .bar {
      width: 5px; 
      height: 20px; 
      background-color: white; 
      margin-right: 10px; 
    }
    .icon {
      width: 20px; /* 小方框的宽度 */
      height: 20px; /* 小方框的高度 */
      // border: 1px solid #4998f2; /* 小方框的边框 */
      margin-left: 5px; /* 小方框之间的间距 */
    }
    .icon:hover {
      color: #4998f2;
    }
    .icon:focus {
      color: #4998f2;
    }
  }
  .addAreaContent {
    padding: 10px;
    display: flex;
    flex-direction: column;
    background-color: #285383;
    font-size: 0.8rem;
    .dialog-content {
      h2 {
        font-size: 1rem;
        font-weight: 400;
        margin-top: 0rem;
      }
      input {
        margin-bottom: 10px;
        padding: 10px;
        width: 94%;
        border: 1px solid #ccc;
        border-radius: 5px;
      }
      button {
        padding: 10px 20px;
        margin-right: 10px;
        border: none;
        border-radius: 5px;
      }
      button:hover {
        opacity: 0.8;
      }
    }
  }
}
</style>
